﻿#region Usings

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using Castle.Components.Binder;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Interfaces.Activity;
using Rodunu.Movies.Interfaces.Movie;
using Rodunu.Movies.Interfaces.Subscriptions;
using Rodunu.Movies.Interfaces.User;
using Rodunu.Movies.Logic.Activity;
using Rodunu.Movies.Logic.Movie;
using Rodunu.Movies.Logic.Subscriptions;
using Rodunu.Movies.Logic.User;
using Rodunu.Movies.Services.NetFlix.DataProvider.Logic;
using Rodunu.Movies.Services.NetFlix.DataProvider.NFService;
using Rodunu.Movies.Web.Utility;

#endregion

namespace Rodunu.Movies.Web.Controllers
{
    public class HomeController : UserBaseController
    {
        #region Private Members

        private readonly IUserActivity _userActivityLogic;
        private readonly ISiteUpdates _siteUpdates;

        #endregion

        #region Controllers

        public HomeController()
        {
            _userActivityLogic = new UserActivityLogic();
            _siteUpdates = new SiteUpdatesLogic();
        }

        #endregion

        #region Home

        public void Index()
        {
            SetUser();
            SetPage(PageTypes.Home);
            SetLayout(true);

            PropertyBag["User"] = User;
            PropertyBag["LastLoginDate"] = LastLoginDate;

            GetActivity();
        }

        #endregion

        #region Activity

        private void GetActivity()
        {
            var freindsActivity = new ActivityBuilder(User, _userActivityLogic);
            PropertyBag["FriendActivity"] = freindsActivity.GetFriendActivity();
            PropertyBag["Height"] = freindsActivity.Height;
            GetUserActivity();
            GetSiteUpdates();
        }

        #region SiteUpdates

        private void GetSiteUpdates()
        {
            var siteUpdates = _siteUpdates.GetRecentSiteUpdates();

            var activityString = new StringBuilder();
            activityString.Append("<table id='NewsUpdates' style='display:none' class='FrontPage'>");

            foreach (var siteUpdate in siteUpdates)
            {
                activityString.Append("<tr>");
                activityString.Append(@"<td class='Title'>");
                activityString.Append(@"<span class='Link' onclick=""GetSiteUpdate('");
                activityString.Append(siteUpdate.Id);
                activityString.Append(@"')"">");
                activityString.Append(siteUpdate.Name);
                activityString.Append("</span>");
                activityString.Append("<span class='Time'>");

                if (DateTime.Now.Subtract(siteUpdate.DateAdded).Days > 7)
                {
                    activityString.Append(siteUpdate.DateAdded.ToShortDateString());
                }
                else
                {
                    if (DateTime.Now.Subtract(siteUpdate.DateAdded).Days == 0)
                    {
                        if (DateTime.Now.Subtract(siteUpdate.DateAdded).Hours == 0)
                        {

                            activityString.Append(DateTime.Now.Subtract(siteUpdate.DateAdded).Minutes);
                            activityString.Append(" minutes ago");
                        }
                        else
                        {
                            activityString.Append(DateTime.Now.Subtract(siteUpdate.DateAdded).Hours);
                            activityString.Append(" hours ago");
                        }
                    }
                    else
                    {
                        activityString.Append(DateTime.Now.Subtract(siteUpdate.DateAdded).Days);
                        activityString.Append(" days ago");
                    }
                }
                activityString.Append("</span>");
                activityString.Append("</td>");
                activityString.Append("</tr>");
            }
            activityString.Append("</table>");
            PropertyBag["SiteUpdates"] = activityString;
        }

        #endregion

        private void GetUserActivity()
        {
            var userActivities = _userActivityLogic.GetActivityForUser(User.Id);
            var activityString = new StringBuilder();
            activityString.Append("<table id='UserActivity' style='display: none' class='FrontPage'>");
            int counter = 0;

            foreach (var activity in userActivities)
            {
                activityString.Append("<tr>");
                activityString.Append("<td class='Title'>");
                if (activity.UserInfo.ShowProfile)
                {
                    activityString.Append("You");
                }
                else
                    activityString.Append(activity.UserInfo.UserName);
                activityString.Append(" ");
                switch (activity.ActivityType)
                {
                    case (int)UserActivityTypes.AddFriend:
                        activityString.Append("added friend ");
                        IFriends friendsLogic = new FriendsLogic();
                        Friends friends = friendsLogic.Find(activity.ActivityId);
                        if (friends.Friend.ShowProfile)
                        {
                            activityString.Append("<a href='../User/UserInfo.ashx?userName=");
                            activityString.Append(friends.Friend.UserName);
                            activityString.Append("'>");
                            activityString.Append(friends.Friend.UserName);
                            activityString.Append("</a>");
                        }
                        else
                            activityString.Append(friends.Friend.UserName);
                        break;
                    case (int)UserActivityTypes.AddMovie:
                        activityString.Append("added ");
                        GetMovie(activity.ActivityId, ref activityString);
                        break;
                    case (int)UserActivityTypes.EditMovie:
                        activityString.Append("edited ");
                        GetMovie(activity.ActivityId, ref activityString);
                        break;
                    case (int)UserActivityTypes.LinkMovieToIMDB:
                        activityString.Append("linked ");
                        GetMovie(activity.ActivityId, ref activityString);
                        activityString.Append(" to IMDB.");
                        break;
                    case (int)UserActivityTypes.RemoveMovie:
                        activityString.Append(activity.Name);
                        break;
                    case (int)UserActivityTypes.UpdateProfile:
                        activityString.Append("updated profile.");
                        break;
                }
                //activityString.Append("</td>");
                activityString.Append("<span class='Time'>");


                if (DateTime.Now.Subtract(activity.DateAdded).Days > 7)
                {
                    activityString.Append(activity.DateAdded.ToShortDateString());
                }
                else
                {
                    if (DateTime.Now.Subtract(activity.DateAdded).Days == 0)
                    {
                        if (DateTime.Now.Subtract(activity.DateAdded).Hours == 0)
                        {

                            activityString.Append(DateTime.Now.Subtract(activity.DateAdded).Minutes);
                            activityString.Append(" minutes ago");
                        }
                        else
                        {
                            activityString.Append(DateTime.Now.Subtract(activity.DateAdded).Hours);
                            activityString.Append(" hours ago");
                        }
                    }
                    else
                    {
                        activityString.Append(DateTime.Now.Subtract(activity.DateAdded).Days);
                        activityString.Append(" days ago");
                    }
                }

                activityString.Append("</span>");
                activityString.Append("</tr>");

                counter++;
            }
            activityString.Append("</table>");
            PropertyBag["UserActivity"] += activityString.ToString();
        }

        #endregion

        private static void GetMovie(long movieId, ref StringBuilder activityString)
        {
            if (activityString == null) throw new ArgumentNullException("activityString");
            IUserMovies movies = new UserMoviesLogic();
            var movie = new UserMovies();
            try
            {
                movie = movies.Find(movieId);
            }
            catch (Exception)
            {
                //TODO: log exception
                return;
            }
            if (movie != null && movie.Id > 0)
            {
                activityString.Append("<a href='../Movie/Detail.ashx?id=");
                activityString.Append(movie.Id.ToString());
                try
                {
                    if (movie.UserInfo != null)
                    {
                        activityString.Append("&UserId=");
                        activityString.Append(movie.UserInfo.Id.ToString());
                    }
                }
                catch (Exception)
                {

                    //TODO: log exception
                }
                activityString.Append("'>");
                try
                {
                    activityString.Append(movie.Name);
                }
                catch (Exception)
                {
                    //TODO: log exception
                    
                }
                activityString.Append("</a>");
            }
        }
    }
}
